package com.manager.controller;

import com.common.result.BaseResult;
import com.manager.pojo.Brand;
import com.manager.pojo.Goods;
import com.manager.pojo.GoodsCategory;
import com.manager.service.brandService;
import com.manager.service.goodCategoryService;
import com.manager.service.goodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.jws.WebParam;
import java.util.List;

@Controller
@RequestMapping("goods")
@CrossOrigin
public class goodsCategoryController {

    @Autowired
    private goodCategoryService goodCategoryService;

    @Autowired
    private com.manager.service.brandService brandService;

    @Autowired
    private com.manager.service.goodService goodService;
    /**
     * 商品列表
     * @return
     */
    @RequestMapping("category/list")
    public String categoryList(Model model){
        model.addAttribute("gcvList",goodCategoryService.selectCategoryForView());
        return "goods.category/category-list1";
    }

    /**
     * 商品添加
     * @return
     */
    @RequestMapping("category/add")
    public String categoryAdd(Model model){
        /**
         * 查询顶级分类
         * @Param:model
         * @param:parentId
         * @return:List<GoodsCategory>
         */
        List<GoodsCategory> goodsCategories = goodCategoryService.selectCategoryTopList();
        model.addAttribute("gcList",goodsCategories);

        return "goods.category/category-add";
    }

    /**
     * 商品分类->新增分类->级联查询
     * @param parentId
     * @return
     */
    @RequestMapping("category/{parentId}")
    @ResponseBody
    public List<GoodsCategory>selectByParentId(@PathVariable Short parentId){
        System.out.println(goodCategoryService.selectParentByParentId(parentId));
        return goodCategoryService.selectParentByParentId(parentId);
    }

    @RequestMapping("category/save")
    @ResponseBody
    /**
     * 商品分类->新增分类->保存分类
     * @param goodsCategory
     * @return
     */
    public BaseResult categorySave(GoodsCategory goodsCategory){
        int insert = goodCategoryService.categorySave(goodsCategory);
        return  insert>0?BaseResult.success():BaseResult.error();
    }
    @RequestMapping("List")
    public String goodsList(Model model){
        List<GoodsCategory> goodsCategories = goodCategoryService.categoryList();
        model.addAttribute("goodsCategories",goodsCategories);
        List<Brand> brandList = brandService.brandList();
        model.addAttribute("brandList",brandList);

        return "goods/goods-list";
    }

    /**
     * 商品-页面跳转
     */
    @RequestMapping("Add")
    public String goodsAdd(Model model){
        List<GoodsCategory> gcList = goodCategoryService.selectCategoryTopList();
        model.addAttribute("gcList",gcList);
        List<Brand> brandList = brandService.brandList();
        model.addAttribute("brandList",brandList);
        return "goods/good-add";
    }

    @RequestMapping("listForPage")
    @ResponseBody
    public BaseResult selectGoodsListByPage(Goods goods, Integer pageNum, Integer pageSize){
        System.out.println("success");
        return goodCategoryService.selectGoodsListByPage(goods,pageNum,pageSize);
    }

    /**
     * 商品列表->新增商品
     * @param goods
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    public BaseResult goodSave(Goods goods){
        BaseResult save = goodService.save(goods);
        return save;
    }

}
